﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BYManageInfoSystem
{
    public partial class Frm_WhetherDoneMakeInvoice : Form
    {
        private DGBY.BLL.BYCompanyTab m_bllBYCompanyTab = new DGBY.BLL.BYCompanyTab();
        private DGBY.BLL.FactoryDataTab m_bllFactoryDataTab = new DGBY.BLL.FactoryDataTab();
        private DGBY.BLL.BGDComeBackTab m_bllBGDComeBackTab = new DGBY.BLL.BGDComeBackTab();
        private DGBY.BLL.FacMakeInvoiceTab m_bllFacMakeInvoiceTab = new DGBY.BLL.FacMakeInvoiceTab();
        private DGBY.BLL.BGDComeBackGoodsMXTab m_bllBGDComeBackGoodsMXTab = new DGBY.BLL.BGDComeBackGoodsMXTab();

        private List<string> lstContractDone = new List<string>();
        private List<string> lstContractReady = new List<string>();
        private List<string> lstContractNotReady = new List<string>();

        public void InitUltraWinGridSetting()
        {
            this.ultraGrid1.DisplayLayout.Override.RowSelectors = Infragistics.Win.DefaultableBoolean.True;
            this.ultraGrid1.DisplayLayout.Override.RowSelectorNumberStyle = Infragistics.Win.UltraWinGrid.RowSelectorNumberStyle.VisibleIndex;
            this.ultraGrid1.DisplayLayout.Override.RowSelectorWidth = 40;
        }

        public void LocalPrintPreveiw()
        {
            Infragistics.Shared.ResourceCustomizer rc = Infragistics.Win.Printing.Resources.Customizer;//Resources.Customizer;
            rc.SetCustomizedString("PrintPreview_DialogCaption", "打印预览");
            rc.SetCustomizedString("PrintPreview_Tool_Print", "打印(&P)");
            rc.SetCustomizedString("PrintPreview_Tool_ClosePreview", "关闭(&C)");
            rc.SetCustomizedString("PrintPreview_Tool_ContextMenuPreviewZoom", "显示比例");
            rc.SetCustomizedString("PrintPreview_Tool_Current_Page", "当前页");
            rc.SetCustomizedString("PrintPreview_Tool_Exit", "退出(&X)");
            rc.SetCustomizedString("PrintPreview_Tool_First_Page", "第一页");
            rc.SetCustomizedString("PrintPreview_Tool_Go_To", "跳至");
            rc.SetCustomizedString("PrintPreview_Tool_Last_Page", "最后一页");
            rc.SetCustomizedString("PrintPreview_Tool_Next_Page", "下一页");
            rc.SetCustomizedString("PrintPreview_Tool_Previous_Page", "前一页");
            rc.SetCustomizedString("PrintPreview_Tool_Next_View", "下一视图(&N)");
            rc.SetCustomizedString("PrintPreview_Tool_Previous_View", "前一视图(&P)");
            rc.SetCustomizedString("PrintPreview_Tool_Hand_Tool", "手型工具(&H)");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Setup", "页面设置(&U)");
            rc.SetCustomizedString("PrintPreview_Tool_Snapshot_Tool", "快照工具(&S)");
            rc.SetCustomizedString("PrintPreview_Tool_View", "视图(&V)");
            rc.SetCustomizedString("PrintPreview_Tool_Whole_Page", "合适页");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom", "缩放(&Z)");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_In", "放大");

            rc.SetCustomizedString("PrintPreview_Tool_Zoom_Out", "缩小");

            rc.SetCustomizedString("PrintPreview_ToolCategory_Context_Menus", "上下文菜单");
            rc.SetCustomizedString("PrintPreview_ToolCategory_File", "文件");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Menus", "菜单");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Tools", "工具栏");
            rc.SetCustomizedString("PrintPreview_ToolCategory_View", "视图");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Zoom_Mode", "缩放模式");

            rc.SetCustomizedString("PrintPreview_ToolTip_ClosePreview", "关闭");
            rc.SetCustomizedString("PrintPreview_ToolTip_Zoom", "缩放");
            rc.SetCustomizedString("StatusBar_Page_X_OF_X", "页:{0}/{1}");

            rc.SetCustomizedString("CustomizeImg_ToolBar_MenuBar", "菜单");
            rc.SetCustomizedString("CustomizeImg_ToolBar_Standard", "标准");
            rc.SetCustomizedString("CustomizeImg_ToolBar_View", "视图");
            rc.SetCustomizedString("PrintPreview_Tool_File", "文件(&F)");
            rc.SetCustomizedString("PrintPreview_Tool_Tools", "工具(&T)");
            rc.SetCustomizedString("PrintPreview_Tool_Dynamic_Zoom_Tool", "动态缩放工具(&D)");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_Out_Tool", "缩小工具");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_In_Tool", "放大工具");
            // rc.SetCustomizedString("PrintPreview_Tool_Page_Layout","菜单");
            rc.SetCustomizedString("PreviewRowColSelection_Cancel", "取消");
            rc.SetCustomizedString("PreviewRowColSelection_SelectedPages", "{0} x {1} 页");

            rc.SetCustomizedString("PreviewRowColSelection_Cancel", "取消");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Width", "页宽");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_MarginWidth", "文字宽度");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_PageWidth", "页宽");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_WholePage", "合适页");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Layout", "页面布局");
            rc.SetCustomizedString("PrintPreview_Tool_Margin_Width", "文字宽度");
            rc.SetCustomizedString("ContextMenuPreviewHand", "缩放视图");


            rc.SetCustomizedString("PrintPreview_Tool_Reduce_Page_Thumbnails", "缩小");
            rc.SetCustomizedString("PrintPreview_Tool_Show_Page_Numbers", "显示页号");
            rc.SetCustomizedString("PrintPreview_Tool_ContextMenuThumbnail", "缩略图");
            rc.SetCustomizedString("PrintPreview_Tool_Enlarge_Page_Thumbnails", "放大");
            rc.SetCustomizedString("PrintPreview_Tool_Thumbnails", "缩略图");
            rc.SetCustomizedString("PrintPreview_Tool_Continuous", "连续排序");
            //说明
            rc.SetCustomizedString("StatusBar_DynamicZoom_Instructions", "单击并拖动进行缩放操作");
            rc.SetCustomizedString("StatusBar_Page_X_OF_X", "当前页: {0} / {1}");
            rc.SetCustomizedString("StatusBar_SnapShot_Instructions", "单击并拖动,系统将选定矩型区域复制到剪帖板");
            rc.SetCustomizedString("StatusBar_ZoomIn_Instructions", "单击并拖动,系统将放大选定矩型区域");
            rc.SetCustomizedString("StatusBar_ZoomOut_Instructions", "单击并拖动,系统将缩小选定矩型区域");
            rc.SetCustomizedString("StatusBar_Hand_Instructions", "单击并拖动以便显示更多内容");
        }

        public Frm_WhetherDoneMakeInvoice()
        {
            InitializeComponent();
        }

        private void Frm_WhetherDoneMakeInvoice_Load(object sender, EventArgs e)
        {
            InitCombox();
            LocalPrintPreveiw();
            InitUltraWinGridSetting();
        }

        private void InitCombox()
        {
            DataSet ds = m_bllBYCompanyTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBox2Comp.DataSource = ds.Tables[0];
                this.comboBox2Comp.DisplayMember = "经营单位名称";
                this.comboBox2Comp.ValueMember = "BYCompanyTabID";
                this.comboBox2Comp.SelectedIndex = 0;
            }

            ds = m_bllFactoryDataTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxFac.DataSource = ds.Tables[0];

                this.comboBoxFac.ValueMember = "FactoryDataTabID";
                this.comboBoxFac.DisplayMember = "工厂名称";
                this.comboBoxFac.SelectedIndex = 0;
            }
        }

        private void InitThreeList()
        {
            Dictionary<string, int> dicContract2Index = new Dictionary<string, int>();
            ultraGrid1.DataSource = null;
            lstContractReady.Clear();
            lstContractNotReady.Clear();
            lstContractDone.Clear();
            DataSet ds = m_bllBGDComeBackTab.ShowAllContractRecByCondition(@"PaidanRegisterTab.BYCompanyTabID = '" + this.comboBox2Comp.SelectedValue + "'"
                                                                           + " and FacConPeoAndTelTab.FactoryDataTabID = '" + this.comboBoxFac.SelectedValue + "'"
                                                                           + " and BGDComeBackTab.SBDate >= '" + this.dateTimePickerFrom.Value + "'" + " and BGDComeBackTab.SBDate <= '" + this.dateTimePickerTo.Value + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                ds.Tables[0].Columns.Add("准备做开票明细", System.Type.GetType("System.String"));
                ds.Tables[0].Columns.Add("已做完开票明细", System.Type.GetType("System.String"));
                int i = 0;
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    //this.listBoxNotReady.Items.Add(dr["合同编号"]);
                    //this.listBoxNotReady.SelectedIndex = 0;
                    lstContractNotReady.Add(dr["合同编号"].ToString());
                    lstContractDone.Add(dr["合同编号"].ToString());
                    dr["准备做开票明细"] = "否";
                    dr["已做完开票明细"] = "是";
                    dicContract2Index.Add(dr["合同编号"].ToString(), i);
                    i++;
                }
            }

            DataSet ds1 = m_bllBGDComeBackTab.ShowAllReadyContractRecByCondition(@"PaidanRegisterTab.BYCompanyTabID = '" + this.comboBox2Comp.SelectedValue + "'"
                                                                           + " and FacConPeoAndTelTab.FactoryDataTabID = '" + this.comboBoxFac.SelectedValue + "'"
                                                                           + " and BGDComeBackTab.SBDate >= '" + this.dateTimePickerFrom.Value + "'" + " and BGDComeBackTab.SBDate <= '" + this.dateTimePickerTo.Value + "'");

            if (ds1.Tables.Count > 0 && ds1.Tables[0].Rows.Count > 0)
            {
                
                foreach (DataRow dr in ds1.Tables[0].Rows)
                {
                    if (!lstContractReady.Contains(dr["合同编号"].ToString()))
                    {
                        
                        lstContractReady.Add(dr["合同编号"].ToString());
                        int nIndex = dicContract2Index[dr["合同编号"].ToString()];
                        ds.Tables[0].Rows[nIndex]["准备做开票明细"] = "是";
                        ds.Tables[0].Rows[nIndex]["已做完开票明细"] = "是";

                    }
                }
            }


            DataSet ds2 = m_bllBGDComeBackTab.ShowAllReadyContractRecByCondition("FacMakeInvoiceTab.TuiShuiLv = 0.00");
            if (ds2.Tables.Count > 0 && ds2.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds2.Tables[0].Rows)
                {
                    if (lstContractDone.Contains(dr["合同编号"].ToString()))
                    {
                        lstContractDone.Remove(dr["合同编号"].ToString());
                        int nIndex = dicContract2Index[dr["合同编号"].ToString()];
                        ds.Tables[0].Rows[nIndex]["已做完开票明细"] = "否";
                    }
                }
            }

            ultraGrid1.DataSource = ds.Tables[0];
            ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
        }
    
        private void buttonQuery_Click(object sender, EventArgs e)
        {
            InitThreeList();
            this.listBoxNotReady.Items.Clear();
            this.listBoxReady.Items.Clear();
            int nCount = lstContractReady.Count;
            for (int i = 0; i < nCount; i++)
            {
                string strTemp = lstContractReady[i].ToString();
                if (!lstContractDone.Contains(strTemp))
                    this.listBoxReady.Items.Add(strTemp);
            }

            nCount = lstContractNotReady.Count;
            for (int i = 0; i < nCount; i++)
            {
                string strTemp = lstContractNotReady[i].ToString();
                if (!lstContractReady.Contains(strTemp))
                    this.listBoxNotReady.Items.Add(strTemp);
            }
        }

        private void buttonGoOne_Click(object sender, EventArgs e)
        {
            if (this.listBoxNotReady.SelectedIndex >= 0)
            {
                this.listBoxReady.Items.Add(this.listBoxNotReady.Items[this.listBoxNotReady.SelectedIndex]);
                this.listBoxNotReady.Items.RemoveAt(this.listBoxNotReady.SelectedIndex);
            }

        }

        private void button3BackOne_Click(object sender, EventArgs e)
        {
            if (this.listBoxReady.SelectedIndex >= 0)
            {
                this.listBoxNotReady.Items.Add(this.listBoxReady.Items[this.listBoxReady.SelectedIndex]);
                this.listBoxReady.Items.RemoveAt(this.listBoxReady.SelectedIndex);
            }

        }

        private void button2GoAll_Click(object sender, EventArgs e)
        {
            int nCount = this.listBoxNotReady.Items.Count;
            for (int i = 0; i < nCount; i++)
            {
                this.listBoxReady.Items.Add(this.listBoxNotReady.Items[i]);
            }
            for (int i = nCount - 1; i >= 0; i--)
            {
                this.listBoxNotReady.Items.RemoveAt(i);
            }
        }

        private void button4BackAll_Click(object sender, EventArgs e)
        {
            int nCount = this.listBoxReady.Items.Count;
            for (int i = 0; i < nCount; i++)
            {
                this.listBoxNotReady.Items.Add(this.listBoxReady.Items[i]);
            }
            for (int i = nCount - 1; i >= 0; i--)
            {
                this.listBoxReady.Items.RemoveAt(i);
            }
        }

        private void InsertOneRecordToFacMakeInvoiceTab(string strContract)
        {
            DataSet ds = m_bllBGDComeBackGoodsMXTab.GetBGDComeBackMXID("PaidanRegisterTab.ContractCode = '" + strContract + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    int nBGDComeBackMXID = Convert.ToInt32(dr["BGDComeBackGoodsMXTabID"].ToString(), 10);
                    m_bllFacMakeInvoiceTab.InsertOneRecordToFacMakeInvoiceTab(nBGDComeBackMXID);
                }
            }
        }

        private void DeleteOneRecordToFacMakeInvoiceTab(string strContract)
        {
                        
            DataSet ds = m_bllBGDComeBackGoodsMXTab.GetBGDComeBackMXID("PaidanRegisterTab.ContractCode = '" + strContract + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    int nBGDComeBackMXID = Convert.ToInt32(dr["BGDComeBackGoodsMXTabID"].ToString(), 10);
                    m_bllFacMakeInvoiceTab.DeleteOneRecordToFacMakeInvoiceTab(nBGDComeBackMXID);
                }
            }
            
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            int nCount = this.listBoxReady.Items.Count;
            for (int i = 0; i < nCount; i++)
            {
                string strTemp = this.listBoxReady.Items[i].ToString();
                if (!lstContractReady.Contains(strTemp))
                    InsertOneRecordToFacMakeInvoiceTab(strTemp);
            }
            nCount = lstContractReady.Count;
            for (int i = 0; i < nCount; i++)
            {
                string strTemp = lstContractReady[i];
                if (!this.listBoxReady.Items.Contains(strTemp))
                    DeleteOneRecordToFacMakeInvoiceTab(strTemp);
            }

            InitThreeList();
        }

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ultraGrid1.DataSource;

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                }
            }
        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {
            this.ultraGridPrintDocument1.Grid = this.ultraGrid1;
            this.ultraPrintPreviewDialog1.Document = this.ultraGridPrintDocument1;
            this.ultraPrintPreviewDialog1.ShowDialog(this);
        }


    }
}